package com.jeesite.modules.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * Web配置类，用于配置静态资源映射和CORS跨域支持
 */
@Configuration
public class WebConfigLowercase implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        System.out.println("=== WebConfig: 正在配置静态资源映射 ===");
        String userDir = System.getProperty("user.dir");
        System.out.println("当前工作目录(user.dir): " + userDir);
        // 配置头像资源映射
        registry.addResourceHandler("/js/avatars/**")
                .addResourceLocations("file:${user.dir}/userfiles/upload/avatars/")
                .setCachePeriod(3600);

        // 配置用户文件资源映射
        registry.addResourceHandler("/js/userfiles/upload/files/**")
                .addResourceLocations("file:${user.dir}/userfiles/upload/files/")
                .setCachePeriod(3600);

        // 配置文件上传资源映射
//        registry.addResourceHandler("/js/userfiles/upload/files/**")
//                .addResourceLocations("file:${user.dir}/userfiles/upload/files/")
//                .setCachePeriod(3600);

        // 通用用户文件映射
        registry.addResourceHandler("/userfiles/**")
                .addResourceLocations("file:${user.dir}/userfiles/")
                .setCachePeriod(3600);

        System.out.println("=== WebConfig: 静态资源映射配置完成 ===");
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        System.out.println("=== WebConfig: 正在配置CORS跨域支持 ===");

        registry.addMapping("/**")
                .allowedOriginPatterns("http://localhost:5173", "http://127.0.0.1:5173","http://localhost:5174", "http://127.0.0.1:5174")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(false)
                .maxAge(3600);

        System.out.println("=== WebConfig: CORS跨域配置完成 ===");
    }
}
